<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <meta name="csrf_token" id="token" content="{{ csrf_token() }}">
    @include('public.h5_head')
    <title>在售商品</title>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/commonjs/rem.js"></script>
    <link rel="stylesheet" type="text/css" href="{{ env('CDN_DOMAIN_NAME') }}/css/commoncss/common.css"/>
    <link rel="stylesheet" type="text/css" href="{{ env('CDN_DOMAIN_NAME') }}/css/sale.css?v=1.0.1"/>
</head>
<script src="{{ env('CDN_DOMAIN_NAME') }}/js/commonjs/4.2.5iscroll.js"></script>
<script src="{{ env('CDN_DOMAIN_NAME') }}/js/app.js"></script>
<body>
<div id="sale_box">
    <script>
        var detail_data=[
                @foreach($datas['details'] as $info)
            [{{ $info->id }},{{ $info->produc_id }},'{{ $info->produc_type }}','{{ $info->produc_val }}',{{ $info->pid }}],
                @endforeach
            [-1,'','','']
        ];
        var brand_name=[
                @foreach($brandName as $k=>$v)
            [{{ $k }},'{{ $v }}'],
                @endforeach
        ];
        var company=[
                @foreach($companys as $k=>$v)
            [{{ $k }},'{{ $v }}'],
            @endforeach
        ];
        function getbrandname(id) {
            for(var i=0;i<brand_name.length;i++)
            {
                if(brand_name[i][0]==id  ){
                    return brand_name[i][1]
                }
            }
        }
        function getcompany(id) {
            for(var i=0;i<company.length;i++)
            {
                if(company[i][0]==id  ){
                    return company[i][1]
                }
            }
        }
        function getGood(pro_id,type) {
            var val='{{ env('PRODUCT_DEFAULT_IMG') }}';
            for(var i=0;i<detail_data.length;i++)
            {
                if(detail_data[i][1]==pro_id && detail_data[i][2] == type && parseInt(detail_data[i][4])==0 ){
                    val =  detail_data[i][3];
                    break;
                }
            }
            return val;
        }
        function getGoodcolor(pro_id,type) {
            var res = "";
            for(var i=0;i<detail_data.length;i++)
            {
                if(detail_data[i][1]==pro_id && detail_data[i][2]==type && parseInt(detail_data[i][4])==0 )
                {
                    res += "<li class='sale_model' title="+pro_id+" id="+detail_data[i][0]+">"+detail_data[i][3]+"</li>";
                }
            }
            return res;
        }
        function getGoodstock(pro_id,type) {
            for(var i=0;i<detail_data.length;i++)
            {
                if(detail_data[i][1]==pro_id && detail_data[i][2] == type){
                    return  "<span  class='sale_stock_"+detail_data[i][1]+"' title="+detail_data[i][3]+"></span>";
                }
            }
        }
        function getGoodsval(pro_id,type) {
            for(var i=0;i<detail_data.length;i++)
            {
                if(detail_data[i][1]==pro_id && detail_data[i][2] == type){
                    return  "<span  class='sale_money_"+detail_data[i][1]+"' title="+detail_data[i][3]+" ></span>";
                }
            }
        }
        function getTotalStock(id) {
            var total = 0;
            for(var i=0;i<detail_data.length;i++)
            {
                if(detail_data[i][1]==id && detail_data[i][2] == 'stock'){
                    total += parseInt(detail_data[i][3]) ;
                }
            }
            return total
        }
//

        $(document).on('click','.sale_model',function () {
            $('.sale_model').css('border','1px solid #bfbfbf');
            $(this).css('border','1px solid red');
            var pid =  $(this).attr('title');
            var id = $(this).attr('id');
            $('.sale_'+pid).hide();
            for(var i=0;i<detail_data.length;i++)
            {
                if(detail_data[i][4] == id && detail_data[i][2] == 'stock' && detail_data[i][1] == pid)
                {
                    $('.sale_stock_'+pid).text("数量:"+detail_data[i][3]);
                }
                if(detail_data[i][4] == id && detail_data[i][2] == 'price' && detail_data[i][1] == pid)
                {
                    $('.sale_money_'+pid).html("价格:<em>￥"+detail_data[i][3]+"</em>");
                }
            }
        });
    </script>
    <div class="wrap" id="#wrapper">
        <div class="main_list">
            <div class="other_list">
                <!--循环开始-->
                @if(count($datas['products'])>0)
                    @foreach($datas['products'] as $k => $v)
                <div class="sale">
                    <div class="sale_icon">
                        <img src="img/00004.png" alt="" />
                        <span>{{ $brandName[$v->brand] }}</span>
                        @if($datas['type'])
                        <span style="float:right;color:#383838;">所属：{{ $companys[$v->company_id] }}</span>
                        @endif
                    </div>
                    <div class="sale_bottom" id="{{ $v->id }}">
                        <div class="sale_pic">
                            @if($v->face)
                                <img src="{{ env('CDN_DOMAIN_NAME') }}/upload/{{$v->face}}" />
                            @else
                                <img src="{{ env('CDN_DOMAIN_NAME') }}/upload/default.png" />
                            @endif
                        </div>
                        <div class="sale_info">
                            <div class="sale_name">{{ $v->title }}</div>
                            <div class="sale_color">
                                <ul>
                                    <script>document.write(getGoodcolor('{{$v->id}}','color'));</script>
                                </ul>
                            </div>
                            <div class="sale_id">款号：{{ $v->girard }}</div>
                            <span class="count_sale sale_{{$v->id}}">总手数：<script>document.write(getTotalStock('{{$v->id}}'));</script></span>
                            <div class="sale_sp">
                                <div class="sale_sum">
                                    <script>document.write(getGoodstock('{{$v->id}}','stock'));</script>
                                </div>
                                <div class="sale_price">
                                    <script>document.write(getGoodsval('{{$v->id}}','price'));</script>
                                </div>
                            </div>
                        </div>
                        <div class="sale_message"><a class="message_a" href="/produc?good_id={{ $v->id }}">点击查看</a></div>
                    </div>
                </div>
                    @endforeach
                @endif
                <!--循环结束-->
            </div>
            {{--加载--}}
            <div class="more" abc="">
                <div class="more_center">
                    <i class="pull_icon"></i><span>上拉加载...</span>
                </div>
            </div>

        </div>
    </div>
</div>
</body>
<script>
    var kp = '{{ $datas['skip'] }}';
    $(".more").attr("abc",kp);
    /*滚动*/
    var myscroll = new iScroll("#wrapper",{
        onScrollMove:function(){
            if (this.y<(this.maxScrollY)) {
                $('.pull_icon').addClass('flip');
                $('.pull_icon').removeClass('loading');
                $('.more span').text('释放加载...');
            }else{
                $('.pull_icon').removeClass('flip loading');
                $('.more span').text('上拉加载...')
            }
        },
        onScrollEnd:function(){
            if ($('.pull_icon').hasClass('flip')) {
                $('.pull_icon').addClass('loading');
                $('.more span').text('加载中...');
                pullUpAction();
            }

        },
        onRefresh:function(){
            $('.more').removeClass('flip');
            $('.more span').text('上拉加载...');
        }
    });
    function pullUpAction(){
        var skip=$(".more").attr("abc");
        $.ajax({
            url: '/apiGetProductInfo',
            type: 'post',
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
            data: {
                skip: skip
            },
            dataType: 'json',
            success: function (data) {
                if(data.success){
                    $(".more").attr("abc",data.info.skip);
                    var obj = Object.keys(data.info.products)
                    if(obj.length > 0)
                    {

                        var arr=data.info.products;
                        var path = '{{ env('CDN_DOMAIN_NAME') }}';
                        for(var i in arr){

                            if(data.info.type)
                                var types = '<span style="float:right;color:#383838;">所属：'+getcompany(arr[i].company_id)+'</span>'
                            else
                                var types = '';
                            var html = '<div class="sale">' +
                                            '<div class="sale_icon">'+
                                                '<img src="img/00004.png" alt="" />'+
                                                '<span>'+getbrandname(arr[i].brand)+'</span>' + types+ '</div>'+
                                            '<div class="sale_bottom" id='+arr[i].id+'>'+
                                                '<div class="sale_pic">'+
                                                    '<img src="'+path+'/upload/'+arr[i].face+'" />'+
                                                '</div>'+
                                                '<div class="sale_info">'+
                                                    '<div class="sale_name">'+arr[i].title+'</div>'+
                                                    '<div class="sale_color">'+
                                                        '<ul>'+getGoodcolor(arr[i].id,'color')+'</ul>'+
                                                    '</div>'+
                                                    '<div class="sale_id">款号：'+arr[i].girard+'</div>'+
                                                    '<span class="count_sale sale_'+arr[i].id+'">总手数：'+getTotalStock(arr[i].id)+'</span>'+
                                                    '<div class="sale_sp">'+
                                                        '<div class="sale_sum">'+getGoodstock(arr[i].id,'stock') +'</div>'+
                                                        '<div class="sale_price">'+getGoodsval(arr[i].id,'price') +'</div>'+
                                                    '</div>'+
                                                '</div>'+
                                                    '<div class="sale_message"><a class="message_a" href="/produc?good_id='+arr[i].id+'">点击查看</a></div>'+
                                            '</div>'+
                                        '</div>'
                            $('.other_list').append(html)
                        }
                    }else{
                        $(".more_center").empty();
                        $(".more_center").text("我是有底线的");
                    }
                    myscroll.refresh();
                }else{

                }
            }
        });
    }
    if ($('.main_list').height()<$('.wrap').height()) {
        $('.more').hide();
        myscroll.destroy();
    }
    /*颜色选择*/
    $(document).on('click','.sale_model',function () {
        $(this).css({"background":"#ea294b","color":"#fff"})
        $(this).siblings().css({"background":"#e9e9e9","color":"#383838"})
    });
//    /*剩余总数*/
//    var count_num=0;
//    var arr=[];
//    for(var k=0;k<detail_data.length;k++){
//        if(detail_data[k][2]=='stock'){
//            if(arr[detail_data[k][1]]){
//                arr[detail_data[k][1]] = parseInt(arr[detail_data[k][1]]) + parseInt(detail_data[k][3]) ;
//            }else{
//                arr[detail_data[k][1]] = parseInt(detail_data[k][3]);
//            }
//        }
//    }
//    for (var i in arr){
//        $(".sale_"+i).text("总手数："+arr[i]);
//    }
</script>
</html>
